#include "CAnimation.h"

#include <stdarg.h>
#include <iostream>

CAnimation::CAnimation()
{
    bRepeat             = false;
    iCurrentFrame       = 0;
    iFramesPerSecond    = 1;
}

CAnimation::~CAnimation()
{
    Release();
}

bool CAnimation::Release()
{
    return true;
}

bool CAnimation::Ended()
{
    return (bRepeat)? false : iCurrentFrame >= (int)Frames.size();
}

void CAnimation::Update()
{
    Accum.Update();
    if (Accum.Ended()){
        unsigned int uiFrameInterval = (iFramesPerSecond)? 1000/iFramesPerSecond : 1;

        int iSum = (int)(Accum.uiTimeAccum / uiFrameInterval);

        if (bRepeat){
            iCurrentFrame += iSum;
            iCurrentFrame %= Frames.size();
        }else
            iCurrentFrame += (iCurrentFrame < (char)Frames.size())? iSum : 0;

        Accum.Restart();
    }
}

int CAnimation::GetCurrentFrame()
{
    return (iCurrentFrame >= 0 && iCurrentFrame < (int)Frames.size())? Frames[iCurrentFrame] : Frames[Frames.size()-1];
}

void CAnimation::Reset()
{
    Accum.Init((iFramesPerSecond) ? 1000/iFramesPerSecond : 1);
    iCurrentFrame = 0;
}

void CAnimation::SetFrameInterval(int iFrameInterval,unsigned int uiTimeAccum)
{
    iFramesPerSecond = iFrameInterval;
    Accum.Init((iFramesPerSecond) ? 1000/iFramesPerSecond : 1);
    Accum.uiTimeAccum = uiTimeAccum;
}
